package com.mdinfo.base.utils;


import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/21 0021.
 */
public class TimeUtil {

    public static String convert(Date date){

        //传入时间和当前时间相差的秒数
        long difference = ((new Date().getTime()) - date.getTime())/1000;
        if(difference<60){
            return "1分钟前";
        }else if(difference<60*60){
            long minute = difference/60;
            return minute+"分钟前";
        }else if(difference<60*60*24){
            long hour = difference/(60*60);
            return hour+"小时前";
        }else if(difference<60*60*24*2){
            return "昨天";
        }else if(difference<60*60*24*3){
            return "前天";
        }else {
            FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd");
            return dateFormat.format(date);
        }
    }


    /**
     * 格式化时间 yyyy-MM-dd HH:mm:ss
     */
    public static String formater(Date date){
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 格式化时间 yyyy-MM-dd
     */
    public static String formater1(Date date){
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        return format.format(date);
    }


    /**
     * 格式化时间 yyyy/mm/dd
     */
    public static String formater2(Date date){
        FastDateFormat format = FastDateFormat.getInstance("yyyy/MM/dd");
        return format.format(date);
    }

    /**
     * 格式化时间 yyyy-MM-dd HH:mm
     */
    public static String formater3(Date date){
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    public static Date parse(String timeStr){
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse1(String timeStr){
        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        try {
            return format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
